package tech.konata.styleslite.rendering.shaders;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Matrix4f;

import java.awt.*;

public class Shaders {

    public static RectangleShader RECT;


    public static void initShaders() {
        RECT = new RectangleShader();
    }

    public static void setupRender() {
        RenderSystem.enableBlend();
        RenderSystem.defaultBlendFunc();
        RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
    }

    public static void setRectanglePoints(BufferBuilder buffer, Matrix4f matrix, float x, float y, float x1, float y1) {
        buffer.vertex(matrix, x, y, 0).endVertex();
        buffer.vertex(matrix, x, y1, 0).endVertex();
        buffer.vertex(matrix, x1, y1, 0).endVertex();
        buffer.vertex(matrix, x1, y, 0).endVertex();
    }

    public static void preShaderDraw(PoseStack matrices, float x, float y, float width, float height) {
        setupRender();
        BufferBuilder buffer = Tesselator.getInstance().getBuilder();
        Matrix4f matrix = matrices.last().pose();
        buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION);
        setRectanglePoints(buffer, matrix, x, y, x + width, y + height);
    }

    public static void endRender() {
        RenderSystem.disableBlend();
        RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
    }

    public static void drawRect(PoseStack matrices, float x, float y, float width, float height, float radius, Color color) {
        preShaderDraw(matrices, x, y, width, height);
        RECT.setParameters(x, y, width, height, radius, color);
        RECT.use();
        Tesselator.getInstance().end();
        endRender();
    }
}
